import time

from binance.um_futures import UMFutures
import logging
from binance.lib.utils import config_logging

from datetime import datetime

config_logging(logging, logging.INFO)

key = "lSfS73A8jxifi2btCmE7fPnhrg5Zf333pyGZkuuwHvZ7GrZg11gyE52SrfY0Kmms"
secret = "8CUn0fvnmJ7ufDZ0BuVuMzu2jOkOb9vfQ077p3iQTyL6IL6WquQa3AgQ8BIW1cAS"

um_futures_client = UMFutures(key=key, secret=secret)
#
# base_url = 'https://fapi.binance.com'
#
# symbol_list = um_futures_client.exchange_info()
# # symbol_list= {'symbols':[{'symbol':'BONDUSDT'}]}
# big_funding_rate = []
# for um_symbol in symbol_list['symbols']:
#     symbol = um_symbol['symbol']
#     # if symbol != 'BONDUSDT':
#     #     continue
#
#     # print("start : {}",symbol)
#
#     # if 'BTCUSD' != symbol:
#     #     continue
#     total_fundingRate = 0.0
#     funding_rate_list = um_futures_client.funding_rate(symbol, startTime=1704038400000,endTime=1722614399000,limit=1000)
#     for fr in funding_rate_list:
#         fundingRate = float(fr['fundingRate'])
#         total_fundingRate += fundingRate
#         if abs(fundingRate) > 0.015:
#             # fr['funding_rate_time_str'] = datetime.fromtimestamp(fr['fundingTime']/1000)
#             print('symbol:{}  time:{}  funding_rate:{}'.format(symbol,datetime.fromtimestamp(fr['fundingTime']/1000),fundingRate))
#             # big_funding_rate.append(fr)

    # print("total_f:" + str(total_fundingRate))
#
# for r in big_funding_rate:
#     print("symbol:{}  time :{}  fundingrate:{}".format(r['symbol'],r['funding_rate_time_str'],r['fundingRate']))


def get_all_symbole():
    symbol_list = um_futures_client.exchange_info()
    result = []
    for _symbol in symbol_list['symbols']:
        result.append(_symbol['symbol'])
    return result


if __name__ == '__main__':
    symbol_list = get_all_symbole()
    for symbol in symbol_list:
        # symbol = 'DARUSDT'
        loop_time = 3
        #2024-1-1
        start_time = 1704038400000
        #2024-10-1
        # start_time = 1727712000000
        interval = 1000*60*60*24*150
        # if symbol != 'BONDUSDT':
        #     # print(symbol)
        #     continue
        #end_time + 150 天
        end_time = 0
        total_fr = 0.0
        while loop_time > 0:
            loop_time -= 1
            end_time = start_time + interval
            funding_rate_list = um_futures_client.funding_rate(symbol, startTime=start_time,endTime=end_time,limit=1000)
            for fr in funding_rate_list:
                fundingRate = float(fr['fundingRate'])
                # if abs(fundingRate) > 0.0035:
                #     # fr['funding_rate_time_str'] = datetime.fromtimestamp(fr['fundingTime']/1000)
                #     print('symbol:{}  time:{}  funding_rate:{}'.format(symbol, datetime.fromtimestamp(fr['fundingTime'] / 1000),
                #                                                        fundingRate))
                #     # big_funding_rate.append(fr)
                total_fr +=fundingRate
            start_time = end_time
            time.sleep(2)
        print("symbol {}   fr:{}".format(symbol,total_fr))


